package com.longrise.android.util;

import android.content.Context;
import android.os.Handler;

import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.UploadURI;
import com.longrise.LEAP.Base.Service.LEAPServiceClient;
import com.longrise.LEAP.Base.Util.ProcessHandler;
import com.longrise.android.Global;

import java.io.File;

/**
 * 图片上传工具类
 * Created by chengsan on 17/8/2.
 */

public class UploadImageUtil {


    /**
     * 上传图片
     * @param context 上下文对象
     * @param namePath 通道名，后台提供，默认为default
     * @param path 图片的本地路径
     * @param onuploadListener 回调接口
     */
    public static void uploadImage(final Context context,final String namePath,final String path,
                                   final OnuploadListener onuploadListener,final ProcessHandler processHandler)
    {

        if(null == namePath || namePath.isEmpty() || path == null || path.isEmpty())
        {
            if(onuploadListener != null)
            {
                onuploadListener.onUploadResult(false,null,null);
            }
            return;
        }
        final Handler handler = new Handler();
        new Thread(new Runnable() {
            @Override
            public void run() {

                String serviceUrl = Global.getInstance().getServerUrl() + "LEAP/Service/RPC/RPC.DO";
                LEAPServiceClient client = new LEAPServiceClient(serviceUrl,false);
                final String resultStr = client.uploadFile(new File(path), "rt=o&path=" + namePath + "&sid=" + client.getsid(), false, new ProcessHandler() {
                    @Override
                    public void onStart(final long l) {

                        if(processHandler != null && handler != null)
                        {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {

                                    processHandler.onStart(l);
                                }
                            });
                        }
                    }

                    @Override
                    public void onProcess(final long l, final long l1) {

                        if(processHandler != null && handler != null)
                        {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {

                                    processHandler.onProcess(l,l1);
                                }
                            });
                        }
                    }

                    @Override
                    public void onComplete() {

                        if(processHandler != null && handler != null)
                        {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {

                                    processHandler.onComplete();
                                }
                            });
                        }
                    }

                    @Override
                    public void onError(final String s, final Exception e) {

                        if(processHandler != null && handler != null)
                        {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {

                                    processHandler.onError(s,e);
                                }
                            });
                        }
                    }

                    @Override
                    public boolean getCancle()
                    {
                        if(processHandler != null && handler != null)
                        {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {

                                    processHandler.getCancle();
                                }
                            });
                        }

                        return false;
                    }
                });
                if(resultStr != null)
                {
                    UploadURI uploadURI = (UploadURI) JSONSerializer.getInstance().DeSerialize(resultStr, UploadURI.class);
                    if(uploadURI != null)
                    {
                        final String tempServiceUrl = Global.getInstance().getServerUrl() +
                                "LEAP/Download/" + uploadURI.getNameedPath() + "/" + uploadURI.getName();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {

                                if(onuploadListener != null)
                                {
                                    onuploadListener.onUploadResult(false,tempServiceUrl,resultStr);
                                }
                            }
                        });

                        return;
                    }

                }

                handler.post(new Runnable() {
                    @Override
                    public void run() {

                        if(onuploadListener != null)
                        {
                            onuploadListener.onUploadResult(false,null,null);
                        }
                    }
                });

            }
        }).start();
    }

    /**
     * 上传图片
     * @param context 上下文对象
     * @param namePath 通道名，后台提供，默认为default
     * @param path 图片的本地路径
     * @param onuploadListener 回调接口
     */
    public static void uploadImage(final Context context,final String namePath,final String path,
                                   final OnuploadListener onuploadListener)
    {

        if(null == namePath || namePath.isEmpty() || path == null || path.isEmpty())
        {
            if(onuploadListener != null)
            {
                onuploadListener.onUploadResult(false,null,null);
            }
            return;
        }
        final Handler handler = new Handler();
        new Thread(new Runnable() {
            @Override
            public void run() {

                String serviceUrl = Global.getInstance().getServerUrl() + "LEAP/Service/RPC/RPC.DO";
                LEAPServiceClient client = new LEAPServiceClient(serviceUrl,false);
                final String resultStr = client.uploadFile(new File(path),"rt=o&path="+namePath+"&sid=" + client.getsid(),false);
                if(resultStr != null)
                {
                    UploadURI uploadURI = (UploadURI) JSONSerializer.getInstance().DeSerialize(resultStr, UploadURI.class);
                    if(uploadURI != null)
                    {
                        final String tempServiceUrl = Global.getInstance().getServerUrl() +
                                "LEAP/Download/" + uploadURI.getNameedPath() + "/" + uploadURI.getName();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {

                                if(onuploadListener != null)
                                {
                                    onuploadListener.onUploadResult(false,tempServiceUrl,resultStr);
                                }
                            }
                        });

                        return;
                    }

                }

                handler.post(new Runnable() {
                    @Override
                    public void run() {

                        if(onuploadListener != null)
                        {
                            onuploadListener.onUploadResult(false,null,null);
                        }
                    }
                });

            }
        }).start();
    }

    /**
     * 字符串转UploadURI数组
     * @param string
     * @return
     */
    public static UploadURI[] string2UploadURIs(String string)
    {
        if(string == null || string.isEmpty())
        {
            return null;
        }
        UploadURI[] uploadURIs = (UploadURI[])JSONSerializer.getInstance().DeSerialize(string,UploadURI[].class);
        return uploadURIs;
    }

    /**
     * 字符串转UploadURI对象
     * @param string
     * @return
     */
    public static UploadURI string2UploadURI(String string)
    {
        if(null == string || string.isEmpty())
        {
            return null;
        }

        UploadURI uploadURI = (UploadURI) JSONSerializer.getInstance().DeSerialize(string, UploadURI.class);
        return uploadURI;
    }

    /**
     * 根据UploadURI获取地址
     * @param uploadURI
     * @return
     */
    public static String getDownLoadUrl(UploadURI uploadURI)
    {
        if(null == uploadURI)
        {
            return null;
        }
        String tempServiceUrl = Global.getInstance().getServerUrl() +
                "LEAP/Download/" + uploadURI.getNameedPath() + "/" + uploadURI.getName();
        return tempServiceUrl;
    }


    public interface OnuploadListener
    {
        /**
         * 上传图片回调方法
         * @param result 是否上传成功
         * @param url 图片在服务器的完整路径
         */
        void onUploadResult(boolean result, String url, String uploadResult);
    }
}
